package com.example.api.controller;

import com.example.api.utils.RandomUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

@RestController
@RequestMapping("/api/email")
public class EmailController {

    @Resource
    private JavaMailSender mailSender;

    @Value("${spring.mail.username}")
    private String from;

    @GetMapping("/send")
    public String send() throws MessagingException {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true);

        //发件人


        message.setFrom("2797116235@qq.com");
        //收件人
        message.setTo("2486681786@qq.com");
        message.setSubject("验证码");
        message.setText("你的验证码为: 123456 ");
        try {
            mailSender.send(mimeMessage);
            return "发送普通邮件成功";
        } catch (MailException e) {
            e.printStackTrace();
            return "邮件方失败";
        }
    }

}
